package com.zhilong.selenium.service;

import com.zhilong.selenium.component.PurchaseBrowserDriver;
import com.zhilong.selenium.entity.ChromePurchase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ChromeRushPurchaseServiceImpl implements ChromeRushPurchaseService {


    @Override
    public String rush2Purchase(ChromePurchase chromePurchase) {
        String url = chromePurchase.getUrl();
        String size = chromePurchase.getSize();
        String color = chromePurchase.getColor();
        if (url == null || url.isEmpty()) {
            return "缺少天猫抢鞋链接";
        }
        if (size == null || size.isEmpty()) {
            return "缺少抢鞋尺码";
        }
        if (color == null || color.isEmpty()) {
            return "缺少抢鞋颜色";
        }
        WebDriver chromeDriver = PurchaseBrowserDriver.getCurrentChromeDriver();
/*        chromeDriver.get("https://www.tmall.com");
        WebElement href = chromeDriver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/p/a[1]"));
        href.click();*/
        // J_loginIframe
/*            String loginUrl = chromeDriver.getCurrentUrl();
            while (chromeDriver.getCurrentUrl().contentEquals(loginUrl)) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }*/
            chromeDriver.get(url);
            WebElement shoesSizeUl = chromeDriver.findElement(By.xpath("//*[@id=\"J_DetailMeta\"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul"));
            List<WebElement> aElements = shoesSizeUl.findElements(By.xpath(".//li/a"));
            aElements.forEach(webElement -> {
                if (webElement.getText().trim().equals(size.trim())) {
                    webElement.click();
                }
            });
            WebElement shoesColorUl = chromeDriver.findElement(By.xpath("/html/body/div[5]/div/div[2]/div/div[1]/div[1]/div/div[4]/div/div/dl[2]/dd/ul"));
            List<WebElement> colorElements = shoesColorUl.findElements(By.xpath(".//li/a"));
            colorElements.forEach(webElement -> {
                System.out.println(webElement.getText() + " color : " + color);
                if (webElement.getText().trim().contains(color.trim())) {
                    System.out.println(webElement.getText());
                    webElement.click();
                }
            });
            WebElement addCartBtn = chromeDriver.findElement(By.xpath("//*[@id=\"J_LinkBasket\"]"));
            addCartBtn.click();
/*            WebElement cart = chromeDriver.findElement(By.xpath("//*[@id=\"J_MUIMallbar\"]/div/div[2]/div[10]/div[3]"));
            cart.click();*/
        // chromeDriver.close();

        return "运行成功！";
    }

    @Override
    public void gotologinPage(){
        WebDriver chromeDriver = PurchaseBrowserDriver.buildChromeNewInstance();
        chromeDriver.get("https://login.tmall.com/");
        chromeDriver.switchTo().frame("J_loginIframe");

        WebElement quicklogin = chromeDriver.findElement(By.id("J_Static2Quick"));
        quicklogin.click();
    }
}
